package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.ClosedBean;
import com.attendance.www.hr.beans.ClosedPageBean;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IClosedService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 封帐时间处理
 * @author 任国庆
 *
 */
@Controller
@RequestMapping("/closed")
public class CloseTimeController {

	@Resource
	private IClosedService closedService;

	/**
	 * 获取封帐信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getClosedPageList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getClosedPageList(HttpServletRequest request) {
		String pageNoStr = request.getParameter("pageNo");
		String pageCountStr = request.getParameter("pageCount");
		if (StringUtils.isBlank(pageNoStr)
				|| StringUtils.isBlank(pageCountStr)) {
			throw new ResultFaultException("0_00001");
		}
		Integer pageNo=Integer.parseInt(pageNoStr);
		Integer pageCount=Integer.parseInt(pageCountStr);
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		ClosedPageBean closedPageBean=new ClosedPageBean();
		closedPageBean=closedService.getClosedPageList(startNum, pageCount);
		return ResultMap.success(closedPageBean);
	}

	/**
	 * 新增封帐信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveClosed.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveClosed(HttpServletRequest request, @RequestBody ClosedBean closedBean) {
		if (StringUtils.isBlank(closedBean.getClosedYear()+"")
				|| StringUtils.isBlank(closedBean.getClosedMonth()+"")) {
			throw new ResultFaultException("0_00001");
		}
		if(closedService.saveClosedVerify(closedBean)){
			throw new ResultFaultException("0_00010");
		}else{
			if(closedService.saveClosed(closedBean)){
				return ResultMap.success();
			}else{
				throw new ResultFaultException("0_00007");
			}
		}
	}

	/**
	 * 删除封帐信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteClosed.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteClosed(HttpServletRequest request) {
		String closedIdStr = request.getParameter("closedId");
		Integer closedId=Integer.parseInt(closedIdStr);
		if(closedService.deleteClosed(closedId)){
			return ResultMap.success();
		}else{
			throw new ResultFaultException("0_00009");
		}
	}

}
